/**
 * Created by hychangkong on 2019-05-20.
 */
Vue.component('upload-img', {
    props: ["modelname","model"],
    template: '<div class="file_box ">\n' +
        '<span class=\'upload\'>' +
        '<input type="file" name="" ref="input" value="" name="saveFile" @change="tirggerFile($event)"/>' +
        '<img :src="imgUrl" alt="" @click="setInput"/>' +
        '<i class="fa fa-plus-square" v-show="!imgUrl" aria-hidden="true" @click="setInput"></i>' +
        '</span>' +
        '</div>',
    data: function () {
        return {
            form: new FormData,
            URL: '/fileTransfer/upload',
            imgUrl: ''
        }
    },
    watch: {
      model: function (val) {
          this.imgUrl = val;
      }
    },
    methods: {
        tirggerFile: function (event) {
            window.form = this.form;
            var self = this;
            console.log(event.target.files);
            this.form.append('file', event.target.files[0]);
            $.ajax({
                type: "post",
                dataType: "json",
                async: false,
                cache: false,
                url: this.URL,
                processData: false,  // 告诉jQuery不要去处理发送的数据
                contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
                data: this.form,
                //}).then(function (result) {
                success: function (result) {
                    if (result.code == 0) {
                        self.imgUrl = result.path[0];
                        self.$root[self.modelname.split('.')[0]][self.modelname.split('.')[1]] = result.path[0];
                    }
                },

            });
        },
        setInput: function () {
            this.$refs.input.click();
        }
    }
})